package com.sl.biz.modular.restapi.handler;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.google.common.collect.Lists;
import com.sl.auth.core.util.StpClientUtil;
import com.sl.auth.core.util.StpCompositeUtil;
import com.sl.biz.modular.process.controller.request.SquareLakeProcessRunRequest;
import com.sl.biz.modular.process.dao.domain.entity.ProcessRelationConfigDO;
import com.sl.biz.modular.process.service.ProcessRelationConfigBizService;
import com.sl.biz.modular.process.service.ProcessVersionContentBizService;
import com.sl.biz.modular.restapi.controller.request.ApiConfigQueryByIdRequest;
import com.sl.biz.modular.restapi.controller.response.ApiConfigQueryByIdVO;
import com.sl.biz.modular.restapi.controller.response.ApiVersionConfigQueryByApiIdVO;
import com.sl.biz.modular.restapi.dao.entity.ApiConfigDO;
import com.sl.biz.modular.restapi.dao.entity.ApiEntityConfigDO;
import com.sl.biz.modular.restapi.dao.entity.ApiHeaderConfigDO;
import com.sl.biz.modular.restapi.dao.service.ApiConfigDaoService;
import com.sl.biz.modular.restapi.service.ApiConfigBizService;
import com.sl.core.engine.exchange.ApiCallBackService;
import com.sl.core.engine.exchange.rest.ApiConfigDTO;
import com.sl.core.engine.exchange.rest.ApiEntityConfigDTO;
import com.sl.core.engine.exchange.rest.ApiHeaderConfigDTO;
import com.sl.core.engine.exchange.rest.ApiVersionConfigDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;


@Component
@RequiredArgsConstructor
public class ApiHandler implements ApiCallBackService {


    private final ApiConfigDaoService apiConfigDaoService;

    private final ApiConfigBizService apiConfigBizService;

    private final ProcessRelationConfigBizService relationConfigBizService;

    private final ProcessVersionContentBizService processService;

    @InterceptorIgnore(tenantLine = "true")
    @Override
    public List<ApiConfigDTO> publishByQueryList(String baseRouteId) {
        List<ApiConfigDO> apiConfigDOS = apiConfigDaoService.queryRestApiList(baseRouteId);
        return BeanUtil.copyToList(apiConfigDOS, ApiConfigDTO.class);
    }

    @InterceptorIgnore(tenantLine = "true")
    @Override
    public ApiConfigDTO queryById(String apiConfigId) {
        ApiConfigQueryByIdRequest apiConfigQueryByIdRequest = new ApiConfigQueryByIdRequest();
        apiConfigQueryByIdRequest.setId(apiConfigId);
        //获取版本列表
        ApiConfigQueryByIdVO apiConfigQueryByIdVO = apiConfigBizService.queryById(apiConfigQueryByIdRequest);
        if (ObjectUtil.isNull(apiConfigQueryByIdVO)) {
            return null;
        }
        ApiConfigDTO apiConfigDTO = BeanUtil.copyProperties(apiConfigQueryByIdVO, ApiConfigDTO.class);
        Boolean loginValidFlag = apiConfigDTO.getLoginValidFlag();
        if(BooleanUtil.isTrue(loginValidFlag)){
            StpCompositeUtil.checkLoginSort();
        }
        List<ApiVersionConfigQueryByApiIdVO> versionList = apiConfigQueryByIdVO.getVersionList();

        List<ApiVersionConfigDTO> collect = versionList.stream().map(v -> {
            List<ApiHeaderConfigDO> headers = v.getHeaders();
            ApiEntityConfigDO entity = v.getEntity();

            ApiVersionConfigDTO apiVersionConfigDTO = BeanUtil.copyProperties(v, ApiVersionConfigDTO.class);
            apiVersionConfigDTO.setHeaders(BeanUtil.copyToList(headers, ApiHeaderConfigDTO.class));
            apiVersionConfigDTO.setEntity(BeanUtil.copyProperties(entity, ApiEntityConfigDTO.class));
            return apiVersionConfigDTO;
        }).collect(Collectors.toList());
        apiConfigDTO.setVersionList(collect);
        return apiConfigDTO;
    }

    @Override
    public Object run(String apiConfigId, String tid, Object params) {

        //找到关联的流程
        List<ProcessRelationConfigDO> processRelationConfigDOS = relationConfigBizService.queryByBizIdAndBizType(apiConfigId, "API");
        if (CollUtil.isEmpty(processRelationConfigDOS)) {
            return null;
        }
        List<Object> result = Lists.newArrayList();
        for (ProcessRelationConfigDO processRelationConfigDO : processRelationConfigDOS) {
            //接收到消息 需要驱动
            SquareLakeProcessRunRequest runRequest = new SquareLakeProcessRunRequest();
            runRequest.setProcessId(processRelationConfigDO.getProcessId());
            runRequest.setParam(ObjectUtil.isNotNull(params)?params:processRelationConfigDO.getParams());
            runRequest.setTid(tid);
            runRequest.setProcessVersion(processRelationConfigDO.getProcessVersion());
            Object run = processService.run(runRequest);
            result.add(run);
        }
        return CollUtil.size(result) == 1 ? CollUtil.get(result, 0) : result;
    }

}
